package com.ruoyi.zxjd.conference.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.SmsUtil;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.zxjd.conference.domain.ConferenceArrange;
import com.ruoyi.zxjd.conference.domain.ConferenceProgress;
import com.ruoyi.zxjd.conference.mapper.ConferenceProgressMapper;
import com.ruoyi.zxjd.conference.service.IConferenceArrangeService;
import com.ruoyi.zxjd.conference.service.IConferenceProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zxjd.conference.mapper.ConferenceParticipantMapper;
import com.ruoyi.zxjd.conference.domain.ConferenceParticipant;
import com.ruoyi.zxjd.conference.service.IConferenceParticipantService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.Size;

/**
 * 参会名单Service业务层处理
 * 
 * @author lcc
 * @date 2021-02-07
 */
@Service
public class ConferenceParticipantServiceImpl implements IConferenceParticipantService 
{
    @Resource
    private ConferenceParticipantMapper conferenceParticipantMapper;

    @Autowired
    private IConferenceProgressService conferenceProgressService;

    @Resource
    private ConferenceProgressMapper conferenceProgressMapper;

    @Autowired
    private IConferenceArrangeService conferenceArrangeService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询参会名单
     * 
     * @param participantId 参会名单ID
     * @return 参会名单
     */
    @Override
    public ConferenceParticipant selectConferenceParticipantById(Long participantId)
    {
        return conferenceParticipantMapper.selectConferenceParticipantById(participantId);
    }

    /**
     * 查询参会名单列表
     * 
     * @param conferenceParticipant 参会名单
     * @return 参会名单
     */
    @Override
    public List<ConferenceParticipant> selectConferenceParticipantList(ConferenceParticipant conferenceParticipant)
    {
            return conferenceParticipantMapper.selectConferenceParticipantList(conferenceParticipant);
    }

    /**
     * 新增参会名单
     * 
     * @param conferenceProgress 参会名单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertConferenceParticipant(ConferenceProgress conferenceProgress)
    {
        conferenceParticipantMapper.deleteConferenceParticipantByProgressId(conferenceProgress.getProgressId());
        List<ConferenceParticipant> conferenceParticipantList = conferenceProgress.getConferenceParticipantList();
        for (ConferenceParticipant conferenceParticipant : conferenceParticipantList) {
            Long arrangeId = conferenceProgress.getArrangeId();
            Long progressId = conferenceProgress.getProgressId();
            conferenceParticipant.setArrangeId(arrangeId);
            conferenceParticipant.setProgressId(progressId);
            conferenceParticipant.setCreateBy(sysUserService.selectUserByLoginName(ShiroUtils.getLoginName()).getUserName());
            conferenceParticipant.setCreateTime(DateUtils.getNowDate());
            conferenceParticipantMapper.insertConferenceParticipant(conferenceParticipant);
        }
        ConferenceProgress conferenceProgress1 = new ConferenceProgress();
        conferenceProgress1.setStatus("1");
        conferenceProgress1.setProgressId(conferenceProgress.getProgressId());
        conferenceProgress1.setReceiveTime(DateUtils.getNowDate());
        int count = conferenceProgressMapper.updateConferenceProgress(conferenceProgress1);
        int validate = conferenceProgressService.validate(conferenceProgress);
        return count+validate;
    }

    /**
     * 修改参会名单
     * 
     * @param conferenceParticipant 参会名单
     * @return 结果
     */
    @Override
    @Transactional
    public int updateConferenceParticipant(ConferenceParticipant conferenceParticipant)
    {
        conferenceParticipant.setUpdateBy(sysUserService.selectUserByLoginName(ShiroUtils.getLoginName()).getUserName());
        conferenceParticipant.setUpdateTime(DateUtils.getNowDate());
        SysUser sysUser = sysUserService.selectUserByName(conferenceParticipant.getParticipantUser());
        @Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符") String phonenumber = sysUser.getPhonenumber();
        String sms=conferenceParticipant.getParticipantUser()+"您好！在线监督平台提示:您的会议填报信息已被修改，请注意查收";
        SmsUtil.sendMessage(sms,phonenumber);
        return conferenceParticipantMapper.updateConferenceParticipant(conferenceParticipant);
    }

    /**
     * 删除参会名单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteConferenceParticipantByIds(String ids)
    {
        return conferenceParticipantMapper.deleteConferenceParticipantByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除参会名单信息
     * 
     * @param participantId 参会名单ID
     * @return 结果
     */
    @Override
    public int deleteConferenceParticipantById(Long participantId)
    {
        return conferenceParticipantMapper.deleteConferenceParticipantById(participantId);
    }

    @Override
    public int deleteConferenceParticipantByArrangeId(Long arrangeId) {
        return conferenceParticipantMapper.deleteConferenceParticipantByArrangeId(arrangeId);
    }

    /**
     * 新增单个参会人员
     * @param conferenceParticipant
     * @return
     */
    @Override
    public int insertConferenceParticipantOne(ConferenceParticipant conferenceParticipant) {
        conferenceParticipant.setCreateBy(sysUserService.selectUserByLoginName(ShiroUtils.getLoginName()).getUserName());
        conferenceParticipant.setCreateTime(DateUtils.getNowDate());
        return conferenceParticipantMapper.insertConferenceParticipant(conferenceParticipant);
    }

}
